package com.example.controller;


import com.example.pojo.*;
import com.example.service.TeacherFirstPageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin   // 允许跨域访问
public class TeacherFirstPageController {

    @Autowired
    private TeacherFirstPageService teacherFirstPageService;

    /**
     * 更新教师个人信息
     */
    @PostMapping("/updateTeacherInfo")
    public UpdateInfoResult updateInfo(@RequestBody Teacher teacher){
        log.info("更新教师信息：{}",teacher);
        LoginMsg loginMsg = teacherFirstPageService.updateInfo(teacher);
        if(loginMsg!=null&&loginMsg.getMsg()==null){
            return UpdateInfoResult.success(loginMsg);
        }else if(loginMsg==null){
            return UpdateInfoResult.fail("修改失败");
        }
        return UpdateInfoResult.fail(loginMsg.getMsg());
    }

    /**
     * 查询教师所教授的班级
     */
    @GetMapping("/searchClass")
    public Result searchClass(Integer teacherId){
        log.info("查询工号为{}的教师所教授的班级",teacherId);
        List<String> classList = teacherFirstPageService.searchClass(teacherId);
        log.info("{}返回",classList);
        return Result.success(classList);
    }

    /**
     * 查询教师所教授的班级的科目
     */
    @GetMapping("/searchSubject")
    public Result searchSubject(Integer teacherId,String className){
        log.info("查询工号为{}的教师所教班级为{}班的科目",teacherId,className);
        List<String> subjectList = teacherFirstPageService.searchSubject(teacherId, className);
        log.info("{}返回",subjectList);
        return Result.success(subjectList);
    }

    /**
     * 查询教师所教授的班级的科目的学期
     */
    @GetMapping("/searchSemester")
    public Result searchSemester(Integer teacherId,String className,String subjectName){
        log.info("查询工号为{}的教师所教班级为{}班科目为{}的学期",teacherId,className,subjectName);
        List<String> semesterList = teacherFirstPageService.searchSemester(teacherId, className, subjectName);
        log.info("{}返回",semesterList);
        return Result.success(semesterList);
    }

    /**
     * 插入单科成绩
     */
    @PostMapping("/insertSingleScore")
    public Result insertSingleScore(@RequestBody Score score){
        log.info("插入单科成绩：{}",score);
        Result result = teacherFirstPageService.insertSingleScore(score);
        return result;
    }

    /**
     * 批量导入成绩
     */
    @PostMapping("/batchInputScore")
    public ResponseEntity<String> batchInputScore(@RequestParam("file") MultipartFile file) {
        ResponseEntity<String> responseEntity = teacherFirstPageService.batchInputScore(file);
        return responseEntity;
    }
}
